package com.example.a022.myapplication;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 022 on 2018/9/5.
 */

public class DetailActivity extends AppCompatActivity{
    private TextView t1, t2, t3;
    private LinearLayout line1, line2, line3;
    private NonScrollGridView gridView, gridView2;
    private List<String> data;
    private List<String> data2;
    private ObserveScrollView scrollView;
    private WebView webView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.good);

        t1 = (TextView) findViewById(R.id.t1);
        t2 = (TextView) findViewById(R.id.t2);
        t3 = (TextView) findViewById(R.id.t3);
        line1 = (LinearLayout) findViewById(R.id.line1);
        line2 = (LinearLayout) findViewById(R.id.line2);
        line3 = (LinearLayout) findViewById(R.id.line3);
        webView = (WebView) findViewById(R.id.webView);
        //    gridView = (NonScrollGridView) findViewById(R.id.gridView);
        gridView2 = (NonScrollGridView) findViewById(R.id.gridView2);
        scrollView = (ObserveScrollView) findViewById(R.id.scrollView);

        data = new ArrayList<>();
        data.add("http://soso3.gtimg.cn/sosopic/0/11129365531347748413/640");
        data.add("https://img01.sogoucdn.com/app/a/100520093/ca86e620b9e623ff-e7ae36db714776c0-b0158348187351632005e109f7faff29.jpg");
        data.add("http://bj-feiyuantu.oss-cn-beijing.aliyuncs.com/creative/vcg/veer/800water/veer-108434082.jpg");
        data.add("https://img02.sogoucdn.com/app/a/100520093/2ad11b094c93197d-4afaf786506af54b-9fd277aea03c8387ebd35dcfba4ba3dd.jpg");
        data.add("https://img03.sogoucdn.com/app/a/100520093/10e8b9550acde0b8-459f14f6a089053d-e879bbcb128a7b240716f9531571fc2c.jpg");
        data.add("https://i03piccdn.sogoucdn.com/cef9209b75d03a5e");

        data2 = new ArrayList<>();
        data2 = new ArrayList<>();
        data2.add("http://soso3.gtimg.cn/sosopic/0/11129365531347748413/640");
        data2.add("https://img01.sogoucdn.com/app/a/100520093/ca86e620b9e623ff-e7ae36db714776c0-b0158348187351632005e109f7faff29.jpg");
        data2.add("http://bj-feiyuantu.oss-cn-beijing.aliyuncs.com/creative/vcg/veer/800water/veer-108434082.jpg");
        data2.add("https://img02.sogoucdn.com/app/a/100520093/2ad11b094c93197d-4afaf786506af54b-9fd277aea03c8387ebd35dcfba4ba3dd.jpg");
        data2.add("https://img03.sogoucdn.com/app/a/100520093/10e8b9550acde0b8-459f14f6a089053d-e879bbcb128a7b240716f9531571fc2c.jpg");
        data2.add("https://i03piccdn.sogoucdn.com/cef9209b75d03a5e");
        data2.add("http://soso3.gtimg.cn/sosopic/0/11129365531347748413/640");
        data2.add("https://img01.sogoucdn.com/app/a/100520093/ca86e620b9e623ff-e7ae36db714776c0-b0158348187351632005e109f7faff29.jpg");
        data2.add("http://bj-feiyuantu.oss-cn-beijing.aliyuncs.com/creative/vcg/veer/800water/veer-108434082.jpg");
        data2.add("https://img02.sogoucdn.com/app/a/100520093/2ad11b094c93197d-4afaf786506af54b-9fd277aea03c8387ebd35dcfba4ba3dd.jpg");
        data2.add("https://img03.sogoucdn.com/app/a/100520093/10e8b9550acde0b8-459f14f6a089053d-e879bbcb128a7b240716f9531571fc2c.jpg");
        data2.add("https://i03piccdn.sogoucdn.com/cef9209b75d03a5e");

   //     gridView.setAdapter(new CommodityDetailAdapter(DetailActivity.this, data));
        gridView2.setAdapter(new FragmentAdapter(DetailActivity.this, data2));

        webView.loadUrl("https://blog.csdn.net/lowprofile_coding/article/details/77928614");

//        android:focusable="true"
//        android:focusableInTouchMode="true"
        // 获取屏幕高度
        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        final Display display = wm.getDefaultDisplay();

        t1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TabColor();
                t1.setTextColor(Color.BLACK);
                int heightTop = line1.getTop();
                Log.d("ss",heightTop+"");
                scrollView.scrollTo(0,heightTop);
            }
        });
        t2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TabColor();
                t2.setTextColor(Color.BLACK);
                int heightTop = line2.getTop();
                Log.d("ss",heightTop+"");
                scrollView.scrollTo(0,heightTop);
            }
        });
        t3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TabColor();
                t3.setTextColor(Color.BLACK);
                int heightTop = line3.getTop();
                Log.d("ss",heightTop+"");
                scrollView.scrollTo(0,heightTop);
            }

        });


        scrollView.setScrollListener(new ObserveScrollView.ScrollListener() {
            @Override
            public void scrollOritention(int l, int t, int oldl, int oldt) {
                Point p = new Point();
                getWindowManager().getDefaultDisplay().getSize(p);
                int screenWidth = p.x;
                int screenHeight = p.y;


                Rect rect = new Rect(0, 0, screenWidth, screenHeight);

                int[] location = new int[2];
                line1.getLocationInWindow(location);
                line2.getLocationInWindow(location);
                line3.getLocationInWindow(location);

                if (line1.getLocalVisibleRect(rect)) {
                    TabColor();
                    t1.setTextColor(Color.BLACK);
                } else {
                    if (line2.getLocalVisibleRect(rect)) {
                        TabColor();
                        t2.setTextColor(Color.BLACK);
                    } else {
                        if (line3.getLocalVisibleRect(rect)) {
                            TabColor();
                            t3.setTextColor(Color.BLACK);
                        } else {
                        }
                    }
                }
            }

        });


    }

    private void TabColor() {
        t1.setTextColor(Color.GRAY);
        t2.setTextColor(Color.GRAY);
        t3.setTextColor(Color.GRAY);
    }


}
